From 0e913cd453f93913062560c9554344fc4f963923 Mon Sep 17 00:00:00 2001 From: tsteven4 Date: Thu, 12 Sep 2013 22:14:07 +0000 Subject: [PATCH] add xmlstreamwriter methods analogous to QXmlStreamWriter that strip illegal characters. git-svn-id: http://gpsbabel.googlecode.com/svn/trunk@4606 f51c46e8-681c-474f-0cfe-069cfd0219fb --- gpsbabel/Makefile.in | 4 +- gpsbabel/gpx.cc | 20 ++----- gpsbabel/reference/LineStyles.gpx | 16 +++--- gpsbabel/src/core/xmlstreamwriter.cc | 86 ++++++++++++++++++++++++++++ gpsbabel/src/core/xmlstreamwriter.h | 59 +++++++++++-------- 5 files changed, 137 insertions(+), 48 deletions(-) create mode 100644 gpsbabel/src/core/xmlstreamwriter.cc diff --git a/gpsbabel/Makefile.in b/gpsbabel/Makefile.in index 9d95cf925..920699750 100644 --- a/gpsbabel/Makefile.in +++ b/gpsbabel/Makefile.in @@ -112,7 +112,7 @@ LIBOBJS = queue.o route.o waypt.o filter_vecs.o util.o vecs.o mkshort.o \ csv_util.o strptime.o grtcirc.o util_crc.o xmlgeneric.o \ formspec.o xmltag.o cet.o cet_util.o fatal.o rgbcolors.o \ inifile.o garmin_fs.o gbsleep.o units.o @GBSER@ gbser.o \ - gbfile.o parse.o session.o \ + gbfile.o parse.o session.o src/core/xmlstreamwriter.o \ $(PALM_DB) $(GARMIN) $(JEEPS) $(SHAPE) @ZLIB@ $(FMTS) $(FILTERS) OBJS = main.o globals.o $(LIBOBJS) @FILEINFO@ @@ -1024,6 +1024,8 @@ smplrout.o: smplrout.cc defs.h config.h queue.h zlib/zlib.h zlib/zconf.h \ filterdefs.h grtcirc.h sort.o: sort.cc defs.h config.h queue.h zlib/zlib.h zlib/zconf.h gbfile.h \ cet.h cet_util.h inifile.h session.h src/core/datetime.h filterdefs.h +src/core/xmlstreamwriter.o: src/core/xmlstreamwriter.cc \ + src/core/xmlstreamwriter.h stackfilter.o: stackfilter.cc defs.h config.h queue.h zlib/zlib.h \ zlib/zconf.h gbfile.h cet.h cet_util.h inifile.h session.h \ src/core/datetime.h filterdefs.h diff --git a/gpsbabel/gpx.cc b/gpsbabel/gpx.cc index b623d25f2..77b9c94ab 100644 --- a/gpsbabel/gpx.cc +++ b/gpsbabel/gpx.cc @@ -1544,22 +1544,12 @@ gpx_write_common_description(const waypoint* waypointp, QString oname) { writer->writeOptionalTextElement("name", oname); - // FIXME: the replace() nonsense here is to prevent bogus control - // characters from being embedded in the output stream. The only place - // this happens in our test suite is a ^Z in the German Garmin GPI test - // file. Filter that out in the two fields below... Ideally, we should - // probably filter that in the input rather than here. - - QString desc = QString::fromUtf8(waypointp->description); - desc = desc.replace(QRegExp("[\014-\032]"), " "); - writer->writeOptionalTextElement("cmt", desc); + writer->writeOptionalTextElement("cmt", waypointp->description); if (waypointp->notes && waypointp->notes[0]) { - QString note = QString::fromUtf8(waypointp->notes); - note = note.replace(QRegExp("[\014-\032]"), " "); - writer->writeTextElement("desc", note); + writer->writeTextElement("desc", waypointp->notes); } else { - writer->writeOptionalTextElement("desc", QString::fromUtf8(waypointp->description)); + writer->writeOptionalTextElement("desc", waypointp->description); } write_gpx_url(waypointp); @@ -1701,8 +1691,8 @@ gpx_route_hdr(const route_head* rte) { fs_xml* fs_gpx; writer->writeStartElement("rte"); - writer->writeOptionalTextElement("name", QString::fromUtf8(rte->rte_name)); - writer->writeOptionalTextElement("desc", QString::fromUtf8(rte->rte_desc)); + writer->writeOptionalTextElement("name", rte->rte_name); + writer->writeOptionalTextElement("desc", rte->rte_desc); if (rte->rte_num) { writer->writeTextElement("number", QString::number(rte->rte_num)); diff --git a/gpsbabel/reference/LineStyles.gpx b/gpsbabel/reference/LineStyles.gpx index ee291e4cb..7db20e3cd 100644 --- a/gpsbabel/reference/LineStyles.gpx +++ b/gpsbabel/reference/LineStyles.gpx @@ -76,23 +76,23 @@ NOTE 1 - 12 test lines - see notes with GPX sample!! four line widths for solid red two line widths for dashed red seven colors for medium width Note that line names have nothing at all to do with line style attributes! These are named for convenient cross-reference, but keep in mind that "red med" is a line label and "Red Medium" is a style label. Waypoints are generated by Topo to exactly match the first track point. Apparently only GPX and KML output from GPSBabel retain track descriptions?? - 12 test lines - see notes with GPX sample!! four line widths for solid red two line widths for dashed red seven colors for medium width Note that line names have nothing at all to do with line style attributes! These are named for convenient cross-reference, but keep in mind that "red med" is a line label and "Red Medium" is a style label. Waypoints are generated by Topo to exactly match the first track point. Apparently only GPX and KML output from GPSBabel retain track descriptions?? + 12 test lines - see notes with GPX sample!! four line widths for solid red two line widths for dashed red seven colors for medium width Note that line names have nothing at all to do with line style attributes! These are named for convenient cross-reference, but keep in mind that "red med" is a line label and "Red Medium" is a style label. Waypoints are generated by Topo to exactly match the first track point. Apparently only GPX and KML output from GPSBabel retain track descriptions?? + 12 test lines - see notes with GPX sample!! four line widths for solid red two line widths for dashed red seven colors for medium width Note that line names have nothing at all to do with line style attributes! These are named for convenient cross-reference, but keep in mind that "red med" is a line label and "Red Medium" is a style label. Waypoints are generated by Topo to exactly match the first track point. Apparently only GPX and KML output from GPSBabel retain track descriptions?? NOTE 2 - how GPX from GPSBabel shows Topo track desc as of 2012: (line name and line style are NOT related to actual color or width) <trk> <name>red line</name> <desc>Style=red med, Width=3, Dashed=0, Color=#ff0000</desc> <number>1</number> <trkseg> ... (Topo menu colors are stored in the file as web-style hex RGB) Red=#ff0000, Yellow=#ffff00, Green=#008000, Blue=#000080, Purple=#800080, Black=#000000, White=#ffffff (Topo solid line widths: Hairline=1, Thin=2, Medium=3, Thick=4) (Topo dashed line widths: Hairline=1, Thin=2) SRE's tpo.c mod stuffs Style/Width/Dashed/Color fields into "desc" field under the assumption that something will post-process GPX or KML output and move information from desc to private extensions (solid lines have "Dashed=0", can have width of 1=hairline to 4=thick) (dashed lines have "Dashed=1", can only have width of 1=hairline or 2=thin) - how GPX from GPSBabel shows Topo track desc as of 2012: (line name and line style are NOT related to actual color or width) <trk> <name>red line</name> <desc>Style=red med, Width=3, Dashed=0, Color=#ff0000</desc> <number>1</number> <trkseg> ... (Topo menu colors are stored in the file as web-style hex RGB) Red=#ff0000, Yellow=#ffff00, Green=#008000, Blue=#000080, Purple=#800080, Black=#000000, White=#ffffff (Topo solid line widths: Hairline=1, Thin=2, Medium=3, Thick=4) (Topo dashed line widths: Hairline=1, Thin=2) SRE's tpo.c mod stuffs Style/Width/Dashed/Color fields into "desc" field under the assumption that something will post-process GPX or KML output and move information from desc to private extensions (solid lines have "Dashed=0", can have width of 1=hairline to 4=thick) (dashed lines have "Dashed=1", can only have width of 1=hairline or 2=thin) + how GPX from GPSBabel shows Topo track desc as of 2012: (line name and line style are NOT related to actual color or width) <trk> <name>red line</name> <desc>Style=red med, Width=3, Dashed=0, Color=#ff0000</desc> <number>1</number> <trkseg> ... (Topo menu colors are stored in the file as web-style hex RGB) Red=#ff0000, Yellow=#ffff00, Green=#008000, Blue=#000080, Purple=#800080, Black=#000000, White=#ffffff (Topo solid line widths: Hairline=1, Thin=2, Medium=3, Thick=4) (Topo dashed line widths: Hairline=1, Thin=2) SRE's tpo.c mod stuffs Style/Width/Dashed/Color fields into "desc" field under the assumption that something will post-process GPX or KML output and move information from desc to private extensions (solid lines have "Dashed=0", can have width of 1=hairline to 4=thick) (dashed lines have "Dashed=1", can only have width of 1=hairline or 2=thin) + how GPX from GPSBabel shows Topo track desc as of 2012: (line name and line style are NOT related to actual color or width) <trk> <name>red line</name> <desc>Style=red med, Width=3, Dashed=0, Color=#ff0000</desc> <number>1</number> <trkseg> ... (Topo menu colors are stored in the file as web-style hex RGB) Red=#ff0000, Yellow=#ffff00, Green=#008000, Blue=#000080, Purple=#800080, Black=#000000, White=#ffffff (Topo solid line widths: Hairline=1, Thin=2, Medium=3, Thick=4) (Topo dashed line widths: Hairline=1, Thin=2) SRE's tpo.c mod stuffs Style/Width/Dashed/Color fields into "desc" field under the assumption that something will post-process GPX or KML output and move information from desc to private extensions (solid lines have "Dashed=0", can have width of 1=hairline to 4=thick) (dashed lines have "Dashed=1", can only have width of 1=hairline or 2=thin) NOTE 3 - how KML from GPSBabel shows Topo track desc as of 2012: (see important notes with GPX sample, all of them apply here!) (note that GPSBabel forces all track styles to be the same - why?) <Folder> <name>Tracks</name> <Folder> <name>red line</name> <snippet/> <description> <![CDATA[<table> <tr><td><b>Description</b> Style=Red Medium, Width=3, Dashed=0, Color=#ff0000 </td></tr> <tr><td><b>Distance</b> 1.3 mi </td></tr> </table>]]> </description> <Folder> <name>Points</name> <Placemark> <name>red line-0</name> ... - how KML from GPSBabel shows Topo track desc as of 2012: (see important notes with GPX sample, all of them apply here!) (note that GPSBabel forces all track styles to be the same - why?) <Folder> <name>Tracks</name> <Folder> <name>red line</name> <snippet/> <description> <![CDATA[<table> <tr><td><b>Description</b> Style=Red Medium, Width=3, Dashed=0, Color=#ff0000 </td></tr> <tr><td><b>Distance</b> 1.3 mi </td></tr> </table>]]> </description> <Folder> <name>Points</name> <Placemark> <name>red line-0</name> ... + how KML from GPSBabel shows Topo track desc as of 2012: (see important notes with GPX sample, all of them apply here!) (note that GPSBabel forces all track styles to be the same - why?) <Folder> <name>Tracks</name> <Folder> <name>red line</name> <snippet/> <description> <![CDATA[<table> <tr><td><b>Description</b> Style=Red Medium, Width=3, Dashed=0, Color=#ff0000 </td></tr> <tr><td><b>Distance</b> 1.3 mi </td></tr> </table>]]> </description> <Folder> <name>Points</name> <Placemark> <name>red line-0</name> ... + how KML from GPSBabel shows Topo track desc as of 2012: (see important notes with GPX sample, all of them apply here!) (note that GPSBabel forces all track styles to be the same - why?) <Folder> <name>Tracks</name> <Folder> <name>red line</name> <snippet/> <description> <![CDATA[<table> <tr><td><b>Description</b> Style=Red Medium, Width=3, Dashed=0, Color=#ff0000 </td></tr> <tr><td><b>Distance</b> 1.3 mi </td></tr> </table>]]> </description> <Folder> <name>Points</name> <Placemark> <name>red line-0</name> ... NOTE 4 - how MapSource 6.16.3 adds color to a track as of 2012: (color choices are Dark Gray, Red, Green, Yellow, Blue, Magenta, Cyan, White, Transparent) (should be easy to turn desc tag into their extension, but better to directly write track styles) <trk> <name>Hwy 50 from Placerville</name> <extensions> <gpxx:TrackExtension xmlns:gpxx="http://www.garmin.com/xmlschemas/GpxExtensions/v3"> <gpxx:DisplayColor>Red</gpxx:DisplayColor> </gpxx:TrackExtension> </extensions> <trkseg> ... - how MapSource 6.16.3 adds color to a track as of 2012: (color choices are Dark Gray, Red, Green, Yellow, Blue, Magenta, Cyan, White, Transparent) (should be easy to turn desc tag into their extension, but better to directly write track styles) <trk> <name>Hwy 50 from Placerville</name> <extensions> <gpxx:TrackExtension xmlns:gpxx="http://www.garmin.com/xmlschemas/GpxExtensions/v3"> <gpxx:DisplayColor>Red</gpxx:DisplayColor> </gpxx:TrackExtension> </extensions> <trkseg> ... + how MapSource 6.16.3 adds color to a track as of 2012: (color choices are Dark Gray, Red, Green, Yellow, Blue, Magenta, Cyan, White, Transparent) (should be easy to turn desc tag into their extension, but better to directly write track styles) <trk> <name>Hwy 50 from Placerville</name> <extensions> <gpxx:TrackExtension xmlns:gpxx="http://www.garmin.com/xmlschemas/GpxExtensions/v3"> <gpxx:DisplayColor>Red</gpxx:DisplayColor> </gpxx:TrackExtension> </extensions> <trkseg> ... + how MapSource 6.16.3 adds color to a track as of 2012: (color choices are Dark Gray, Red, Green, Yellow, Blue, Magenta, Cyan, White, Transparent) (should be easy to turn desc tag into their extension, but better to directly write track styles) <trk> <name>Hwy 50 from Placerville</name> <extensions> <gpxx:TrackExtension xmlns:gpxx="http://www.garmin.com/xmlschemas/GpxExtensions/v3"> <gpxx:DisplayColor>Red</gpxx:DisplayColor> </gpxx:TrackExtension> </extensions> <trkseg> ... red thick diff --git a/gpsbabel/src/core/xmlstreamwriter.cc b/gpsbabel/src/core/xmlstreamwriter.cc new file mode 100644 index 000000000..abfb230d7 --- /dev/null +++ b/gpsbabel/src/core/xmlstreamwriter.cc @@ -0,0 +1,86 @@ +/* + Copyright (C) 2013 Robert Lipe, gpsbabel.org + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111 USA + + */ + +#include +#include + +#include +#if (QT_VERSION < QT_VERSION_CHECK(5, 0, 0)) +#include +#else +#include +#endif +#include + +// As this code began in C, we have several hundred places that write +// c strings. Add a test that the string contains anything useful +// before serializing an empty tag. +// We also strip out characters that are illegal in xml. These can creep +// into our structures from other formats where they are legal. + +namespace gpsbabel +{ + +XmlStreamWriter:: XmlStreamWriter(QString* s) : QXmlStreamWriter(s) {} + +XmlStreamWriter::XmlStreamWriter(QFile* f) : QXmlStreamWriter(f) {} + +#if (QT_VERSION < QT_VERSION_CHECK(5, 0, 0)) +QRegExp XmlStreamWriter::badXml10 = QRegExp("[\\x0000-\\x0008]|[\\x000b-\\x000c]|[\\x000e-\\x001f]"); +#else +QRegularExpression XmlStreamWriter::badXml10 = QRegularExpression("[\\x00-\\x08]|[\\x0b-\\x0c]|[\\x0e-\\x1f]"); +#endif + +// Dont emit the attribute if there's nothing interesting in it. +void XmlStreamWriter::writeOptionalAttribute(const QString& qualifiedName, QString value) +{ + if (!value.isEmpty()) { + QXmlStreamWriter::writeAttribute(qualifiedName, value.replace(badXml10, " ")); + } +} + +// Dont emit the element if there's nothing interesting in it. +void XmlStreamWriter::writeOptionalTextElement(const QString& qualifiedName, QString text) +{ + if (!text.isEmpty()) { + QXmlStreamWriter::writeTextElement(qualifiedName, text.replace(badXml10, " ")); + } +} + +void XmlStreamWriter::writeAttribute(const QString& qualifiedName, QString value) +{ + QXmlStreamWriter::writeAttribute(qualifiedName, value.replace(badXml10, " ")); +} + +void XmlStreamWriter::writeCDATA(QString text) +{ + QXmlStreamWriter::writeCDATA(text.replace(badXml10, " ")); +} + +void XmlStreamWriter::writeCharacters(QString text) +{ + QXmlStreamWriter::writeCharacters(text.replace(badXml10, " ")); +} + +void XmlStreamWriter::writeTextElement(const QString& qualifiedName, QString value) +{ + QXmlStreamWriter::writeTextElement(qualifiedName, value.replace(badXml10, " ")); +} + +} // namespace gpsbabel diff --git a/gpsbabel/src/core/xmlstreamwriter.h b/gpsbabel/src/core/xmlstreamwriter.h index a0239f410..5a15f3a5e 100644 --- a/gpsbabel/src/core/xmlstreamwriter.h +++ b/gpsbabel/src/core/xmlstreamwriter.h @@ -17,34 +17,45 @@ */ -#include -#include +#ifndef XMLSTREAMWRITER_H +#define XMLSTREAMWRITER_H -// As this code began in C, we have several hundred places that write -// c strings. Add a test that the string contains anything useful -// before serializing an empty tag. +#include +#include -namespace gpsbabel { +class QFile; +#if (QT_VERSION < QT_VERSION_CHECK(5, 0, 0)) +class QRegExp; +#else +class QRegularExpression; +#endif + +namespace gpsbabel +{ + +class XmlStreamWriter : public QXmlStreamWriter +{ +private: +#if (QT_VERSION < QT_VERSION_CHECK(5, 0, 0)) + static QRegExp badXml10; +#else + static QRegularExpression badXml10; +#endif -class XmlStreamWriter : public QXmlStreamWriter { public: - XmlStreamWriter(QString* s) : QXmlStreamWriter(s) {} - XmlStreamWriter(QFile* f) : QXmlStreamWriter(f) {} - - // Dont emit the attribute if there's nothing interesting in it. - void writeOptionalAttribute(QString tag, QString value) { - if (!value.isEmpty()) { - writeAttribute(tag, value); - } - } - - // Dont emit the tag if there's nothing interesting in it. - void writeOptionalTextElement(QString tag, QString value) { - if (!value.isEmpty()) { - writeTextElement(tag, value); - } - } + XmlStreamWriter(QString* s); + XmlStreamWriter(QFile* f); + + void writeOptionalAttribute(const QString& qualifiedName, QString value); + void writeOptionalTextElement(const QString& qualifiedName, QString text); + void writeAttribute(const QString& qualifiedName, QString value); + void writeCDATA(QString text); + void writeCharacters(QString text); + void writeTextElement(const QString& qualifiedName, QString value); }; -}; // namespace gpsbabel +} // namespace gpsbabel + +#endif // XMLSTREAMWRITER_H + -- 2.30.2